home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ccm / Conflicts.py < prev    next >
Encoding:
Python Source  |  2009-03-04  |  19.4 KB  |  521 lines

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, 
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17. #
  18. # Authors: Quinn Storm (quinn@beryl-project.org)
  19. #          Patrick Niklaus (marex@opencompositing.org)
  20. #          Guillaume Seguin (guillaume@segu.in)
  21. #          Christopher Williams (christopherw@verizon.net)
  22. # Copyright (C) 2007 Quinn Storm
  23.  
  24. import pygtk
  25. import gtk
  26.  
  27. from ccm.Constants import *
  28. from ccm.Utils import *
  29.  
  30. import locale
  31. import gettext
  32. locale.setlocale(locale.LC_ALL, "")
  33. gettext.bindtextdomain("ccsm", DataDir + "/locale")
  34. gettext.textdomain("ccsm")
  35. _ = gettext.gettext
  36.  
  37. class Conflict:
  38.     def __init__(self, autoResolve):
  39.         self.AutoResolve = autoResolve
  40.  
  41.     # buttons = (text, type/icon, response_id)
  42.     def Ask(self, message, buttons, custom_widgets=None):
  43.         if self.AutoResolve:
  44.             return gtk.RESPONSE_YES
  45.  
  46.         dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_WARNING)
  47.  
  48.         for text, icon, response in buttons:
  49.             button = gtk.Button(text)
  50.             button.set_image(gtk.image_new_from_stock(icon, gtk.ICON_SIZE_BUTTON))
  51.             dialog.add_action_widget(button, response)
  52.  
  53.         if custom_widgets != None:
  54.             for widget in custom_widgets:
  55.                 dialog.vbox.pack_start(widget, False, False)
  56.  
  57.         dialog.set_markup(message)
  58.         dialog.show_all()
  59.         answer = dialog.run()
  60.         dialog.destroy()
  61.  
  62.         return answer
  63.  
  64. class ActionConflict (Conflict):
  65.  
  66.     ActionTypes = set(('Bell', 'Button', 'Edge', 'Key'))
  67.  
  68.     def __init__ (self, setting, settings, autoResolve):
  69.  
  70.         def ExcludeInternal (settings):
  71.             for setting in settings:
  72.                 if not setting.Info[0]:
  73.                     yield setting
  74.  
  75.         Conflict.__init__(self, autoResolve)
  76.         self.Conflicts = []
  77.         self.Name = ""
  78.         self.Setting = setting
  79.  
  80.         if settings is None:
  81.             settings = []
  82.  
  83.         self.Settings = settings
  84.  
  85.         # if the action is internal, include all global actions plus internal
  86.         # actions from the same plugin. If it is global, include all actions.
  87.  
  88.         if not settings:
  89.             for plugin in self.Setting.Plugin.Context.Plugins.values ():
  90.                 if plugin.Enabled:
  91.                     pluginActions = GetSettings(plugin, displayOnly=True,
  92.                                                 types=self.ActionTypes)
  93.  
  94.                     if len(setting.Info) and setting.Info[0] and plugin is not setting.Plugin:
  95.                         settings.extend(ExcludeInternal(pluginActions))
  96.                     else:
  97.                         settings.extend(pluginActions)
  98.  
  99.     def Resolve (self, updater = None):
  100.         if len (self.Conflicts):
  101.             for setting in self.Conflicts:
  102.                 answer = self.AskUser (self.Setting, setting)
  103.                 if answer == gtk.RESPONSE_YES:
  104.                     setting.Value = 'Disabled'
  105.                     if updater:
  106.                         updater.UpdateSetting (setting)
  107.                 if answer == gtk.RESPONSE_NO:
  108.                     return False
  109.  
  110.         return True
  111.  
  112.     def AskUser (self, setting, conflict):
  113.         msg = _("The new value for the %(binding)s binding for the action <b>%(action)s</b> "\
  114.               "in plugin <b>%(plugin)s</b> conflicts with the action <b>%(action_conflict)s</b> of the <b>%(plugin_conflict)s</b> plugin.\n"\
  115.               "Do you wish to disable <b>%(action_conflict)s</b> in the <b>%(plugin_conflict)s</b> plugin?")
  116.  
  117.         msg_dict = {'binding': self.Name,
  118.                     'action': setting.ShortDesc,
  119.                     'plugin': setting.Plugin.ShortDesc,
  120.                     'action_conflict': conflict.ShortDesc,
  121.                     'plugin_conflict': conflict.Plugin.ShortDesc}
  122.  
  123.         msg = msg % protect_markup_dict (msg_dict)
  124.  
  125.         yesButton    = (_("Disable %(action_conflict)s") % msg_dict,  gtk.STOCK_YES,  gtk.RESPONSE_YES)
  126.         noButton     = (_("Don't set %(action)s") %  msg_dict,    gtk.STOCK_NO,   gtk.RESPONSE_NO)
  127.         ignoreButton = (_("Set %(action)s anyway") % msg_dict,    gtk.STOCK_STOP, gtk.RESPONSE_REJECT)
  128.  
  129.         return self.Ask (msg, (ignoreButton, noButton, yesButton))
  130.  
  131. class KeyConflict(ActionConflict):
  132.     def __init__(self, setting, newValue, settings=None, autoResolve=False, ignoreOld=False):
  133.         ActionConflict.__init__(self, setting, settings, autoResolve)
  134.         self.Name = _("key")
  135.  
  136.         if not newValue:
  137.             return
  138.  
  139.         newValue = newValue.lower ()
  140.         oldValue = self.Setting.Value.lower ()
  141.         badValues = ["disabled", "none"]
  142.         if not ignoreOld:
  143.             badValues.append (oldValue)
  144.         if newValue in badValues:
  145.             return
  146.  
  147.         for s in self.Settings:
  148.             if s is setting:
  149.                 continue
  150.             if s.Type == 'Key':
  151.                 if s.Value.lower() == newValue:
  152.                     self.Conflicts.append (s)
  153.  
  154. class ButtonConflict(ActionConflict):
  155.     def __init__(self, setting, newValue, settings=None, autoResolve=False, ignoreOld=False):
  156.         ActionConflict.__init__(self, setting, settings, autoResolve)
  157.         self.Name = _("button")
  158.  
  159.         if not newValue:
  160.             return
  161.  
  162.         newValue = newValue.lower ()
  163.         oldValue = self.Setting.Value.lower ()
  164.         badValues = ["disabled", "none"]
  165.         if not ignoreOld:
  166.             badValues.append (oldValue)
  167.         if newValue in badValues:
  168.             return
  169.  
  170.         for s in self.Settings:
  171.             if s is setting:
  172.                 continue
  173.             if s.Type == 'Button':
  174.                 if s.Value.lower() == newValue:
  175.                     self.Conflicts.append (s)
  176.  
  177. class EdgeConflict(ActionConflict):
  178.     def __init__(self, setting, newValue, settings=None, autoResolve=False, ignoreOld=False):
  179.         ActionConflict.__init__(self, setting, settings, autoResolve)
  180.         self.Name = _("edge")
  181.  
  182.         if not newValue:
  183.             return
  184.  
  185.         newEdges = set(newValue.split("|"))
  186.  
  187.         if not ignoreOld:
  188.             oldEdges = set(self.Setting.Value.split("|"))
  189.             diff = newEdges - oldEdges
  190.             if diff:
  191.                newEdges = diff # no need to check edges that were already set
  192.             else:
  193.                 return
  194.  
  195.         for s in self.Settings:
  196.             if s is setting:
  197.                 continue
  198.             elif s.Type == 'Edge':
  199.                 settingEdges = set(s.Value.split("|"))
  200.                 union = newEdges & settingEdges
  201.                 if union:
  202.                     for edge in union:
  203.                         self.Conflicts.append ((s, edge))
  204.                         break
  205.  
  206.     def Resolve (self, updater = None):
  207.         if len (self.Conflicts):
  208.             for setting, edge in self.Conflicts:
  209.                 answer = self.AskUser (self.Setting, setting)
  210.                 if answer == gtk.RESPONSE_YES:
  211.                     value = setting.Value.split ("|")
  212.                     value.remove (edge)
  213.                     setting.Value = "|".join (value)
  214.                     if updater:
  215.                         updater.UpdateSetting (setting)
  216.                 if answer == gtk.RESPONSE_NO:
  217.                     return False
  218.  
  219.         return True
  220.  
  221. # Not used for plugin dependencies (which are handled by ccs) but own feature checking e.g. image support
  222. class FeatureRequirement(Conflict):
  223.     def __init__(self, context, feature, autoResolve=False):
  224.         Conflict.__init__(self, autoResolve)
  225.         self.Requirements = []
  226.         self.Context = context
  227.         self.Feature = feature
  228.  
  229.         self.Found = False
  230.         for plugin in context.Plugins.values():
  231.             if feature in plugin.Features:
  232.                 self.Found = True
  233.                 if not plugin.Enabled:
  234.                     self.Requirements.append(plugin)
  235.     
  236.     def Resolve(self):
  237.         if len(self.Requirements) == 0 and self.Found:
  238.             return True
  239.         elif not self.Found:
  240.             answer = self.ErrorAskUser()
  241.             if answer == gtk.RESPONSE_YES:
  242.                 return True
  243.             else:
  244.                 return False
  245.         
  246.         for plugin in self.Requirements:
  247.             answer = self.AskUser(plugin)
  248.             if answer == gtk.RESPONSE_YES:
  249.                 plugin.Enabled = True
  250.                 self.Context.Write()
  251.                 return True
  252.  
  253.     def ErrorAskUser(self):
  254.         msg = _("You are trying to use the feature <b>%(feature)s</b> which is <b>not</b> provided by any plugin.\n"\
  255.                 "Do you wish to use this feature anyway?")
  256.  
  257.         msg_dict = {'feature': self.Feature}
  258.  
  259.         msg = msg % protect_markup_dict (msg_dict)
  260.  
  261.         yesButton = (_("Use %(feature)s") % msg_dict,       gtk.STOCK_YES, gtk.RESPONSE_YES)
  262.         noButton  = (_("Don't use %(feature)s") % msg_dict, gtk.STOCK_NO,  gtk.RESPONSE_NO)
  263.  
  264.         answer = self.Ask(msg, (noButton, yesButton))
  265.  
  266.         return answer
  267.  
  268.     def AskUser(self, plugin):
  269.         msg = _("You are trying to use the feature <b>%(feature)s</b> which is provided by <b>%(plugin)s</b>.\n"\
  270.                 "This plugin is currently disabled.\n"\
  271.                 "Do you wish to enable <b>%(plugin)s</b> so the feature is available?")
  272.  
  273.         msg_dict = {'feature': self.Feature,
  274.                     'plugin': plugin.ShortDesc}
  275.  
  276.         msg = msg % protect_markup_dict (msg_dict)
  277.  
  278.         yesButton = (_("Enable %(plugin)s") % msg_dict,       gtk.STOCK_YES, gtk.RESPONSE_YES)
  279.         noButton  = (_("Don't enable %(feature)s") % msg_dict, gtk.STOCK_NO,  gtk.RESPONSE_NO)
  280.  
  281.         answer = self.Ask(msg, (noButton, yesButton))
  282.  
  283.         return answer
  284.  
  285. class PluginConflict(Conflict):
  286.     def __init__(self, plugin, conflicts, autoResolve=False):
  287.         Conflict.__init__(self, autoResolve)
  288.         self.Conflicts = conflicts
  289.         self.Plugin = plugin
  290.  
  291.     def Resolve(self):
  292.         for conflict in self.Conflicts:
  293.             if conflict[0] == 'ConflictFeature':
  294.                 answer = self.AskUser(self.Plugin, conflict)
  295.                 if answer == gtk.RESPONSE_YES:
  296.                     disableConflicts = conflict[2][0].DisableConflicts
  297.                     con = PluginConflict(conflict[2][0], disableConflicts,
  298.                                          self.AutoResolve)
  299.                     if con.Resolve():
  300.                         conflict[2][0].Enabled = False
  301.                     else:
  302.                         return False
  303.                 else:
  304.                     return False
  305.  
  306.             elif conflict[0] == 'ConflictPlugin':
  307.                 answer = self.AskUser(self.Plugin, conflict)
  308.                 if answer == gtk.RESPONSE_YES:
  309.                     disableConflicts = conflict[2][0].DisableConflicts
  310.                     con = PluginConflict(conflict[2][0], disableConflicts,
  311.                                          self.AutoResolve)
  312.                     if con.Resolve():
  313.                         conflict[2][0].Enabled = False
  314.                     else:
  315.                         return False
  316.                 else:
  317.                     return False
  318.             
  319.             elif conflict[0] == 'RequiresFeature':
  320.                 answer, choice = self.AskUser(self.Plugin, conflict)
  321.                 if ret == gtk.RESPONSE_YES:
  322.                     for plg in conflict[2]:
  323.                         if plg.ShortDesc == choice:
  324.                             enableConflicts = plg.EnableConflicts
  325.                             con = PluginConflict(plg, enableConflicts,
  326.                                                  self.AutoResolve)
  327.                             if con.Resolve():
  328.                                 plg.Enabled = True
  329.                             else:
  330.                                 return False
  331.                             break
  332.                 else:
  333.                     return False
  334.  
  335.             elif conflict[0] == 'RequiresPlugin':
  336.                 answer = self.AskUser(self.Plugin, conflict)
  337.                 if answer == gtk.RESPONSE_YES:
  338.                     enableConflicts = conflict[2][0].EnableConflicts
  339.                     con = PluginConflict(conflict[2][0], enableConflicts,
  340.                                          self.AutoResolve)
  341.                     if con.Resolve():
  342.                         conflict[2][0].Enabled = True
  343.                     else:
  344.                         return False
  345.                 else:
  346.                     return False
  347.  
  348.             elif conflict[0] == 'FeatureNeeded':
  349.                 answer = self.AskUser(self.Plugin, conflict)
  350.                 if answer == gtk.RESPONSE_YES:
  351.                     for plg in conflict[2]:
  352.                         disableConflicts = plg.DisableConflicts
  353.                         con = PluginConflict(plg, disableConflicts,
  354.                                              self.AutoResolve)
  355.                         if con.Resolve():
  356.                             plg.Enabled = False
  357.                         else:
  358.                             return False
  359.                 else:
  360.                     return False
  361.  
  362.             elif conflict[0] == 'PluginNeeded':
  363.                 answer = self.AskUser(self.Plugin, conflict)
  364.                 if answer == gtk.RESPONSE_YES:
  365.                     for plg in conflict[2]:
  366.                         disableConflicts = plg.DisableConflicts
  367.                         con = PluginConflict(plg, disableConflicts,
  368.                                              self.AutoResolve)
  369.                         if con.Resolve():
  370.                             plg.Enabled = False
  371.                         else:
  372.                             return False
  373.                 else:
  374.                     return False
  375.  
  376.         # Only when enabling a plugin
  377.         types = []
  378.         actionConflicts = []
  379.         if not self.Plugin.Enabled and not self.AutoResolve:
  380.             for setting in GetSettings(self.Plugin):
  381.                 conflict = None
  382.                 if setting.Type == 'Key':
  383.                     conflict = KeyConflict(setting, setting.Value, ignoreOld=True)
  384.                 elif setting.Type == 'Button':
  385.                     conflict = ButtonConflict(setting, setting.Value, ignoreOld=True)
  386.                 elif setting.Type == 'Edge':
  387.                     conflict = EdgeConflict(setting, setting.Value, ignoreOld=True)
  388.  
  389.                 # Conflicts were found
  390.                 if conflict and conflict.Conflicts:
  391.                     name = conflict.Name
  392.                     if name not in types:
  393.                         types.append(name)
  394.                     actionConflicts.append(conflict)
  395.  
  396.         if actionConflicts:
  397.             answer = self.AskUser(self.Plugin, ('ConflictAction', types))
  398.             if answer == gtk.RESPONSE_YES:
  399.                 for conflict in actionConflicts:
  400.                     conflict.Resolve()
  401.  
  402.         return True
  403.  
  404.     def AskUser(self, plugin, conflict):
  405.         msg = ""
  406.         okMsg = ""
  407.         cancelMsg = ""
  408.         widgets = []
  409.  
  410.         # CCSM custom conflict
  411.         if conflict[0] == 'ConflictAction':
  412.             msg = _("Some %(bindings)s bindings of Plugin <b>%(plugin)s</b> " \
  413.                     "conflict with other plugins. Do you want to resolve " \
  414.                     "these conflicts?")
  415.  
  416.             types = conflict[1]
  417.             bindings = ", ".join(types[:-1])
  418.             if len(types) > 1:
  419.                 bindings = "%s and %s" % (bindings, types[-1])
  420.  
  421.             msg_dict = {'plugin': plugin.ShortDesc,
  422.                         'bindings': bindings}
  423.  
  424.             msg = msg % protect_markup_dict (msg_dict)
  425.  
  426.             okMsg     = _("Resolve conflicts") % msg_dict
  427.             cancelMsg = _("Ignore conflicts") % msg_dict
  428.  
  429.         elif conflict[0] == 'ConflictFeature':
  430.             msg = _("Plugin <b>%(plugin_conflict)s</b> provides feature " \
  431.                     "<b>%(feature)s</b> which is also provided by " \
  432.                     "<b>%(plugin)s</b>")
  433.             
  434.             msg_dict = {'plugin_conflict': conflict[2][0].ShortDesc,
  435.                         'feature': conflict[1],
  436.                         'plugin': plugin.ShortDesc}
  437.  
  438.             msg = msg % protect_markup_dict (msg_dict)
  439.  
  440.             okMsg     = _("Disable %(plugin_conflict)s") % msg_dict
  441.             cancelMsg = _("Don't enable %(plugin)s") % msg_dict
  442.         
  443.         elif conflict[0] == 'ConflictPlugin':
  444.             msg = _("Plugin <b>%(plugin_conflict)s</b> conflicts with " \
  445.                     "<b>%(plugin)s</b>.")
  446.             msg = msg % protect_markup_dict (msg_dict)
  447.  
  448.             okMsg = _("Disable %(plugin_conflict)s") % msg_dict
  449.             cancelMsg = _("Don't enable %(plugin)s") % msg_dict
  450.         
  451.         elif conflict[0] == 'RequiresFeature':
  452.             pluginList = ', '.join("\"%s\"" % plugin.ShortDesc for plugin in conflict[2])
  453.             msg = _("<b>%(plugin)s</b> requires feature <b>%(feature)s</b> " \
  454.                     "which is provided by the following " \
  455.                     "plugins:\n%(plugin_list)s")
  456.             
  457.             msg_dict = {'plugin': plugin.ShortDesc,
  458.                         'feature': conflict[1],
  459.                         'plugin_list': pluginList}
  460.  
  461.             msg = msg % protect_markup_dict (msg_dict)
  462.  
  463.             cmb = gtk.combo_box_new_text()
  464.             for plugin in conflict[2]:
  465.                 cmb.append_text(plugin.ShortDesc)
  466.             cmb.set_active(0)
  467.             widgets.append(cmb)
  468.  
  469.             okMsg = _("Enable these plugins")
  470.             cancelMsg_("Don't enable %(plugin)s") % msg_dict
  471.         
  472.         elif conflict[0] == 'RequiresPlugin':
  473.             msg = _("<b>%(plugin)s</b> requires the plugin <b>%(require)s</b>.")
  474.  
  475.             msg_dict = {'plugin': plugin.ShortDesc,
  476.                         'require': conflict[2][0].ShortDesc}
  477.  
  478.             msg = msg % protect_markup_dict (msg_dict)
  479.  
  480.             okMsg = _("Enable %(require)s") % msg_dict
  481.             cancelMsg = _("Don't enable %(plugin)s") % msg_dict
  482.         
  483.         elif conflict[0] == 'FeatureNeeded':
  484.             pluginList = ', '.join("\"%s\"" % plugin.ShortDesc for plugin in conflict[2])
  485.             msg = _("<b>%(plugin)s</b> provides the feature " \
  486.                     "<b>%(feature)s</b> which is required by the plugins " \
  487.                     "<b>%(plugin_list)s</b>.")
  488.             
  489.             msg_dict = {'plugin': plugin.ShortDesc,
  490.                         'feature': conflict[1],
  491.                         'plugin_list': pluginList}
  492.             
  493.             msg = msg % protect_markup_dict (msg_dict)
  494.  
  495.             okMsg = _("Disable these plugins")
  496.             cancelMsg = _("Don't disable %(plugin)s") % msg_dict
  497.         
  498.         elif conflict[0] == 'PluginNeeded':
  499.             pluginList = ', '.join("\"%s\"" % plugin.ShortDesc for plugin in conflict[2])
  500.             msg = _("<b>%(plugin)s</b> is required by the plugins " \
  501.                     "<b>%(plugin_list)s</b>.")
  502.             
  503.             msg_dict = {'plugin': plugin.ShortDesc,
  504.                         'plugin_list': pluginList}
  505.             
  506.             msg = msg % protect_markup_dict (msg_dict)
  507.  
  508.             okMsg = _("Disable these plugins")
  509.             cancelMsg = _("Don't disable %(plugin)s") % msg_dict
  510.  
  511.         okButton     = (okMsg,     gtk.STOCK_OK,     gtk.RESPONSE_YES)
  512.         cancelButton = (cancelMsg, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
  513.         
  514.         answer = self.Ask(msg, (cancelButton, okButton), widgets)
  515.         if conflict[0] == 'RequiresFeature':
  516.             choice = widgets[0].get_active_text()
  517.             return answer, choice
  518.         
  519.         return answer
  520.         e
  521.